﻿using System.Collections.ObjectModel;
using UploadClient.Framework;

namespace UploadClient.ViewModel
{
    public class ConfigurationViewModel : ViewModelBase
    {
        private ConfigurationItemViewModel _SelectedConfigItem;

        public ConfigurationViewModel()
        {
            Initial();

            Delete = new DelegateCommand(Delete_Execute, Delete_CanExecute);
            New = new DelegateCommand(New_Execute);
            Save = new DelegateCommand(Save_Execute, Save_CanExecute);
        }

        public ObservableCollection<ConfigurationItemViewModel> ConfigItems { get; set; }

        public DelegateCommand Delete { get; set; }
        public DelegateCommand Save { get; set; }
        public DelegateCommand New { get; set; }

        public ConfigurationItemViewModel SelectedConfigItem
        {
            get
            {
                return _SelectedConfigItem;
            }
            set
            {
                _SelectedConfigItem = value;
                OnPropertyChanged("SelectedConfigItem");
            }
        }

        private void Initial()
        {
            ConfigItems = new ObservableCollection<ConfigurationItemViewModel>();
        }

        private void Save_Execute(object param)
        {
            //Todo: Save
        }

        private bool Save_CanExecute(object param)
        {
            return SelectedConfigItem.DataChanged;
        }

        private void New_Execute(object param)
        {
            SelectedConfigItem = new ConfigurationItemViewModel();
            ConfigItems.Add(SelectedConfigItem);
        }

        private bool Delete_CanExecute(object param)
        {
            return (SelectedConfigItem != null);
        }
        private void Delete_Execute(object param) {
            ConfigItems.Remove(SelectedConfigItem);
            SelectedConfigItem = null;
        }
    }
}
